package cn.fws.meap.community.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import cn.fws.framework.utils.StringUtils;
import cn.fws.meap.community.enums.AnswerIsAdoptEnum;
import cn.fws.meap.community.enums.DetailsTypeEnum;
import cn.fws.meap.community.enums.ProblemIsSolveEnum;
import cn.fws.meap.community.enums.ResourcesTypeEnum;
import cn.fws.meap.community.mapper.ProblemMapper;
import cn.fws.meap.community.service.*;
import cn.fws.meap.community.vo.AnswerListVo;
import cn.fws.meap.community.vo.Details;
import cn.fws.meap.system.constants.QiNiuParam;
import cn.fws.meap.user.mapper.AccountMapper;
import cn.fws.meap.utils.QiNiuUtil;
import cn.fws.meap.utils.logBug.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fws.framework.utils.ConvertUtils;
import cn.fws.framework.utils.UUIDUtils;

import cn.fws.meap.community.mapper.AnswerMapper;
import cn.fws.meap.community.entity.Answer;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 回答 服务层实现
 *
 * @author chenkh
 * @date 2019-03-07
 */
@Service
@Transactional
public class AnswerServiceImpl implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private DetailsUtils<Answer> detailsUtils;
    @Autowired
    private IResourcesService resourcesService;
    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    private AccountMapper accountMapper;


    /**
     * 查询回答信息
     *
     * @param id 回答ID
     * @return 回答信息
     */
    @Override
    public Answer selectById(String id) {
        return answerMapper.selectById(id);
    }

    /**
     * 查询回答列表
     *
     * @param answer 回答信息
     * @return 回答集合
     */
    @Override
    public List<Details<Answer>> selectList(Answer answer) {
        List<Answer> answerList = answerMapper.selectList(answer);
        List<Details<Answer>> detailsList = new ArrayList<>();
        for (Answer ans : answerList) {
            //获取openId
            String openid = ans.getOpenid();
            //获取回答ID
            String id = ans.getId();
            Details<Answer> details = detailsUtils.getDetails(id, openid, DetailsTypeEnum.IS_ANSWER.getCode());
            detailsList.add(details);
        }
        return detailsList;

    }

    /**
     * 新增回答
     *
     * @param answer 回答信息
     * @return 结果
     */
    @Override
    public String insert(Answer answer) {
        // 设置主键
        answer.setId(UUIDUtils.getUUID());
        // 设置创建时间
        answer.setCreateTime(new java.util.Date());
        answer.setIsAdopt(AnswerIsAdoptEnum.NO_ADOPT.getCode());
        //获取内容存入七牛云

        String answerMsg = answer.getAnswerMsg();
        String address = QiNiuUtil.uploadFile(QiNiuParam.MEAP_DOCUMENT, answerMsg);
        String resourcesAddress = resourcesService.addResources(address, ResourcesTypeEnum.TEXT.getCode(), answer.getOpenid());
        answer.setAnswerMsg(resourcesAddress);
        answerMapper.insert(answer);
        return answer.getId();
    }

    /**
     * 修改回答
     *
     * @param answer 回答信息
     * @return 结果
     */
    @Override
    public int update(Answer answer) {
        // 设置修改时间
        answer.setUpdateTime(new Date());
        return answerMapper.update(answer);
    }

    /**
     * 删除回答对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids) {
        return answerMapper.deleteByIds(ConvertUtils.toStrArray(ids));
    }

    @Override
    public int adopt(String answerId, String problemId, String openId, Integer money) {
        //修改当前问题状态已解决
        int updateSolve = problemMapper.updateSolve(problemId, ProblemIsSolveEnum.SOLVE.getCode());

        if (updateSolve == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        //修改问答已采纳状态
        int updateAdopt = answerMapper.updateAdopt(answerId, AnswerIsAdoptEnum.ADOPT.getCode());

        if (updateAdopt == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        //当前回答账户记录添加流水

        return updateAdopt;

    }

    /**
     * 根据问题ID获取问答内容
     *
     * @param answerId
     * @return
     */
    @SysLog
    @Override
    public List<AnswerListVo> getAnswer(String answerId, String openId, Boolean flag) {
        List<AnswerListVo> twoList = null;
        List<String> list = new ArrayList<>();


        List<AnswerListVo> answerListVos = answerMapper.selectAnswerList(answerId);
        for (AnswerListVo answerListVo : answerListVos) {
            twoList = new LinkedList<>();
            list.add(answerListVo.getId());

            String fileMsg = QiNiuUtil.getFileMsg(answerListVo.getMsgAddress());
            answerListVo.setMsgAddress(fileMsg);

            Boolean aBoolean = evaluateService.selectOpenId(openId, answerListVo.getId());
            //点赞状态
            answerListVo.setEvaluateStatus(aBoolean);

            if (flag || !StringUtils.isNotEmpty(list)) {
                continue;
            }

            //根据一级评论的id 获取到二级评论
            List<AnswerListVo> twoAnswerList = answerMapper.selectTwoAnswerList(list);
            for (AnswerListVo listVo : twoAnswerList) {


                if (listVo.getSuperId().equals(answerListVo.getId())) {
                    String answerMsg2 = listVo.getMsgAddress();
                    String fileMsg2 = QiNiuUtil.getFileMsg(answerMsg2);
                    listVo.setMsgAddress(fileMsg2);
                    twoList.add(listVo);
                }
            }
            answerListVo.setTwoAnswerList(twoList);
        }


        return answerListVos;
    }

    /**
     * 查询是否被采纳过
     *
     * @param problemId
     * @param adopt
     * @return
     */
    @Override
    public Boolean selectByAdopt(String problemId, String adopt) {
        return answerMapper.selectByAdopt(problemId, adopt);
    }

}
